package com.gao.pubsub.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.gao.pubsub.service.ReceiverMessageService;
import com.gao.pubsub.service.ReceiverRedisOnMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import java.util.concurrent.CountDownLatch;

@Configuration
@EnableCaching
public class RedisConfig {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter1,
                                            MessageListenerAdapter listenerAdapter2) {
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory);
        // 订阅频道
        listenerContainer.addMessageListener(listenerAdapter1, new PatternTopic("pubsub"));
        listenerContainer.addMessageListener(listenerAdapter2, new PatternTopic("pubsub"));

        //序列化对象（特别注意：发布的时候需要设置序列化；订阅方也需要设置序列化）
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        serializer.setObjectMapper(objectMapper);

        listenerContainer.setTopicSerializer(serializer);
        return listenerContainer;
    }

    /**
     * 绑定消息监听者和接收监听的方法
     * @return
     */
    @Bean
    public MessageListenerAdapter listenerAdapter1(ReceiverMessageService receiver) {
        return new MessageListenerAdapter(receiver,"receiveMessage");
    }

    /**
     * 绑定消息监听者和接收监听的方法
     * @return
     */
    @Bean
    public MessageListenerAdapter listenerAdapter2(ReceiverRedisOnMessage receiver) {
        return new MessageListenerAdapter(receiver,"onMessage");
    }

    ///**
    // * 注册订阅者
    // * @param latch
    // * @return
    // */
    //@Bean
    //ReceiverMessageService receiver(CountDownLatch latch) {
    //    return new ReceiverMessageService(latch);
    //}

    /**
     * 使用默认的 String redis template
     * @param connectionFactory
     * @return
     */
    @Bean
    StringRedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }
}
